package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.repository.UserInfoRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Override
    public void save(UserInfo user) {
        user.setId(null);
        userInfoRepository.save(user);
    }

    @Override
    public void delete(String id) {
        userInfoRepository.deleteById(id);
    }

    @Override
    public void update(UserInfo user) {
        userInfoRepository.save(user);
    }

    @Override
    public UserInfo get(String id) {
        return userInfoRepository.findById(id).orElse(null);
    }

    @Override
    public List<UserInfo> list() {
        return userInfoRepository.findAll();
    }

    @Override
    public boolean checkPhone(String phone) {
        UserInfo user = userInfoRepository.findByPhone(phone);
        return user==null;
    }

    @Override
    public void sendVerifyCode(String phone) {
        //使用uuid生成验证码
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);
        //拼接短信
        StringBuilder sb = new StringBuilder();
        sb.append("【叩丁狼】你的验证码为").append(code).append(",请在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟内使用");
        //模拟发送验证码
        System.out.println(sb);
        /*//真实发送验证码
        //使用spring提供的RestTemplate类发送请求和别的项目交互,使用京东万象上的api
        RestTemplate restTemplate = new RestTemplate();
        //使用getForObject方法访问api完成功能
        //参数一为请求路径,参数二为响应数据封装成什么数据类型
        //参数三为请求参数,包括手机号,短信内容,使用令牌
        //请求路径
        String url="https://way.jd.com/chuangxin/dxjk?mobile={1}&content={2}&appkey={3}";
        //发送请求,返回成功数据信息,包含success
        String data = restTemplate.getForObject(url, String.class, phone, sb, "e3fb81f9921cc5002567fe7e34b121d3");
        //打印响应信息
        System.out.println(data);*/

        //存储到redis中
        userInfoRedisService.sendVerifyCode(phone,code);

    }

    @Override
    public void regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        ///完成所有数据判空操作
        AssertUtil.haslength(phone,"手机号不能为空");
        AssertUtil.haslength(nickname,"昵称不能为空");
        AssertUtil.haslength(password,"密码不能为空");
        AssertUtil.haslength(rpassword,"确认密码不能为空");
        AssertUtil.haslength(verifyCode,"验证码不能为空");
        //完成手机号格式判断
        if(!phone.matches("1[358][0-9]{9}")){
            throw new LogicException("手机号格式不正确");
        }
        //完成手机号唯一性判断
        if (!this.checkPhone(phone)) {
            throw new LogicException("手机号已经被注册");
        }
        //完成两次密码校验
        AssertUtil.isEquals(password,rpassword,"密码不相同");
        //完成短信验证码校验
        //根据phone获取redis中的值
        String code = userInfoRedisService.getVerifyCode(phone);
        //和用户传来的做对比,需要对code进行判空;
        if (code==null||!code.equals(verifyCode)) {
            throw new LogicException("验证码错误或失效");
        }

        //完成注册功能的实现
        //创建用户对象
        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setPassword(password);
        userInfo.setNickname(nickname);
        userInfo.setCity("");
        userInfo.setEmail("");
        userInfo.setInfo("");
        userInfo.setHeadImgUrl("/images/user.png");
        //重要信息最好显示设置
        userInfo.setState(0);
        this.save(userInfo);
    }

    @Override
    public UserInfo login(String username, String password) {
        AssertUtil.haslength(username,"用户名不能为空");
        AssertUtil.haslength(password,"密码不能为空");
        UserInfo user = userInfoRepository.findByPhone(username);
        if(user==null||!user.getPassword().equals(password)){
            throw new LogicException("用户名或密码错误");
        }
        return user;
    }
}
